package com.dp.appi.dao.impl;

import com.dp.appi.dao.EnvironmentDao;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * Created by bchen on 2016/5/19.
 */
@Component
public class EnvironmentDaoImpl extends BaseDaoImpl implements EnvironmentDao {

    /**
     * 获取踏勘环境信息
     *
     * @param exploration_id
     */
    @Override
    public Map<String, Object> envInfo(Long exploration_id) {
        StringBuffer sql = new StringBuffer();
        sql.append("select environment_id,traffic, ");
        sql.append("width,haul_distance,voice_remark,characters_remark ");
        sql.append("from p_environment ");
        sql.append("where exploration_id = ?");
        return queryForMap(sql.toString(),exploration_id);
    }

    /**
     * 获取踏勘环境信息
     *
     * @param exploration_id
     */
    @Override
    public boolean isExistEnv(Long exploration_id) {
        StringBuffer sql = new StringBuffer();
        sql.append("select count(1) ");
        sql.append("from p_environment ");
        sql.append("where exploration_id = ?");
        return queryForObject(sql.toString(),Integer.class,exploration_id)==0?false:true;
    }

    /**
     * 新增踏勘环境信息
     *
     * @param envMap
     */
    @Override
    public Long addEnvInfo(Map<String, String> envMap) {
        StringBuffer sql = new StringBuffer();
        sql.append("INSERT INTO `p_environment` ");
        sql.append("(`exploration_id`, `traffic`, `width`, `haul_distance`, `voice_remark`, `characters_remark`)  ");
        sql.append("VALUES (:exploration_id, :traffic, :width, :haul_distance, :voice_remark,:characters_remark)");
        return executeSqlReturnID(sql.toString(),envMap);
    }

    /**
     * 修改踏勘环境信息
     *
     * @param envMap
     */
    @Override
    public void upEnvInfo(Map<String, String> envMap) {
        StringBuffer sql = new StringBuffer();
        sql.append("UPDATE `p_environment` ");
        sql.append("SET `exploration_id`=?, ");
        sql.append("`traffic`=?, ");
        sql.append(" `width`=?, ");
        sql.append(" `haul_distance`=?,  ");
        sql.append(" `voice_remark`=?,");
        sql.append(" `characters_remark`=?");
        sql.append("  WHERE (`exploration_id`=?)");
        Object[] param = new Object[]{
            envMap.get("exploration_id"),
            envMap.get("traffic"),
            envMap.get("width"),
            envMap.get("haul_distance"),
            envMap.get("voice_remark"),
            envMap.get("characters_remark"),
            envMap.get("exploration_id"),
        };
        getJdbcTemplate().update(sql.toString(),param);
    }

}
